package io.a.rdd

import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setAppName("RDDFlatMap").setMaster("local[*]")
    val sc = new SparkContext(sparkConf)

    // 创建一个包含10个字符串元素的RDD
    val rdd1 = sc.parallelize(Seq(
      "hello world", "hello java", "hello scala", "hello spark"
    ))
    println("rdd1:")
    rdd1.foreach(println)
    val rdd2 = rdd1.flatMap(_.split(" "))
    println("rdd2:")
    rdd2.foreach(println)
    val rdd3 = rdd2.map((_, 1))
    println("rdd3:")
    rdd3.foreach(println)
    val rdd4 = rdd3.reduceByKey((_ + _))
    println("rdd4:")
    rdd4.foreach(println)
  }
}
